<script setup>
import { ElMessage,ElMessageBox } from 'element-plus'
import { ref } from 'vue'
import AddLevel from './components/AddLevel.vue'

const tableCols = [
  {
    key: 'dealer_level_name',
    label: '等级名称'
  },
  {
    key: 'is_dividend',
    label: '是否分红'
  }
]

const list=ref([])
async function getList() {
  const res = await window.api.getAllDealerLevel()
  list.value = res
}
getList()


const showCreateLevel=ref(false)
const editData=ref(null)
function handleEditLevel(e){
  editData.value=e
  showCreateLevel.value=true
}

async function handleDelLevel(e){
  await ElMessageBox.confirm(
    '删除该等级',
    '删除提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning'
    }
  )
  const res=await window.api.deleteDealerLevel(e.dealer_level_id)
  ElMessage.success('删除成功')
  getList()
}
</script>

<template>
  <div>
    <div class="top-filter-wrap">
      <el-button type="primary" @click="handleEditLevel(null)">新增等级</el-button>
    </div>
    <div class="table-wrap" style="margin-top:20px">
      <el-table :data="list" style="width: 100%" border>
        <el-table-column
          v-for="item in tableCols"
          :key="item.key"
          :prop="item.key"
          :label="item.label"
          align="center"
        >
          <template #default="scope">
            <span v-if="item.key==='is_dividend'">{{scope.row.is_dividend?'是':'否'}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="opt" label="操作" align="center" width="180px">
          <template #default="scope">
            <el-button type="primary" link @click="handleEditLevel(scope.row)">编辑</el-button>
            <el-button type="danger" link @click="handleDelLevel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
        <template #empty>
          <div class="table-no-data">
            <img src="@renderer/assets/img/empty-data.png" />
            <span>暂无数据</span>
          </div>
        </template>
      </el-table>
    </div>
  </div>

  <!-- 创建等级 -->
  <AddLevel v-model:show="showCreateLevel" :editData="editData" @success="getList" />
</template>
